﻿namespace Util.ImageProcess

open System.Drawing
open System.Drawing.Imaging
open System.Drawing.Drawing2D
open System.IO

type ImageProcess =
    static member DefaultQulityLevel =75L
    static member  CompressBitmap (imgInStream:Stream , imgOutStream:Stream,value:int64) =
        let getEncoderInfo ( mimeType) :ImageCodecInfo =
            let  encoders=ImageCodecInfo.GetImageEncoders()
            let r =
                encoders
                |>Array.filter (fun encoder -> encoder.MimeType = mimeType )
            if r.Length>0 then r.[0]
            else null
        let jpegEncoder = getEncoderInfo "image/jpeg"
        let myEncoder =Encoder.Quality
        let p = new EncoderParameter( myEncoder,value)
        let ps = new EncoderParameters(1)
        ps.Param.[0]<-p
        let bitmap = Bitmap.FromStream(imgInStream)
        if bitmap <> null then 
            bitmap.Save(imgOutStream,jpegEncoder,ps)    

    //在保存的时候进行压缩
    static member  CompressBitmap (filePath:string,savePath:string,value:int64)  =
        let getEncoderInfo ( mimeType) :ImageCodecInfo =
            let  encoders=ImageCodecInfo.GetImageEncoders()
            let r =
                encoders
                |>Array.filter (fun encoder -> encoder.MimeType = mimeType )
            if r.Length>0 then r.[0]
            else null
        let jpegEncoder = getEncoderInfo "image/jpeg"
        let myEncoder =Encoder.Quality
        let p = new EncoderParameter( myEncoder,value)
        let ps = new EncoderParameters(1)
        ps.Param.[0]<-p
        let bitmap = Bitmap.FromFile(filePath)
        if bitmap <> null then 
            bitmap.Save(savePath,jpegEncoder,ps)

    static member  DefaultCompressBitmap(filePath:string, saveFilePath:string)  =
        ImageProcess.CompressBitmap(filePath, saveFilePath, ImageProcess.DefaultQulityLevel)
    static member  DefaultCompressBitmap(ins:Stream,outs:Stream)  =
        ImageProcess.CompressBitmap(ins,outs ,ImageProcess.DefaultQulityLevel)

//
//let LoadBitmap fileName =
//    Bitmap.FromFile(fileName) :?> Bitmap


//let CloneButLowerSize (pf:PixelFormat) (bitmap:Bitmap) =
//    let sizeF =bitmap.PhysicalDimension
//    bitmap.Clone(new RectangleF(new PointF(0.0f,0.0f),sizeF),pf)
//    //PixelFormat.Format16bppArgb1555 天空失真
//
//let Scale (persent:float32) (bitmap:Bitmap)=
//    bitmap.SetResolution( 100.0f,100.0f)//bitmap.HorizontalResolution*persent,bitmap.VerticalResolution*persent)
//    bitmap.Clone() :?> Bitmap

//
//let compress (scale:float) (bitmap:Bitmap) =
//  let newPic = new Bitmap( int ((float bitmap.Width)*scale), int ((float bitmap.Height) *scale))
//  newPic.SetResolution(bitmap.HorizontalResolution,bitmap.VerticalResolution)
//  use grap = Graphics.FromImage(newPic);
//  grap.SmoothingMode <- SmoothingMode.HighSpeed;
//  grap.InterpolationMode <- InterpolationMode.Low;
//  grap.CompositingQuality<-CompositingQuality.HighSpeed
//  grap.CompositingMode<- CompositingMode.SourceOver
//  //grap.DrawImage(bitmap, new Rectangle(0, 0, newPic.Width,newPic.Height),new Rectangle(0, 0, bitmap.Width,bitmap.Height),GraphicsUnit.Pixel)
//  grap.DrawImage(bitmap, new Rectangle(0, 0, newPic.Width,newPic.Height))
//  newPic

//
//let f = @"F:\YL\sucai\t8.jpg"
//let f_ = @"F:\YL\sucai\t8_p.jpg"
//let f = @"F:\YL\sucai\t2.jpg"
//let f_ = @"F:\YL\sucai\t2_p.jpg"

//let f = @"F:\YL\sucai\hr\[000054].jpg"
//let f_ = @"F:\YL\sucai\hr\[000054]_p.jpg"
//let f = @"F:\YL\sucai\hr\[000141].jpg"
//let f_ = @"F:\YL\sucai\hr\[000141]_p.jpg"
//let f = @"F:\YL\sucai\hr\[000198].jpg"
//let f_ = @"F:\YL\sucai\hr\[000198]_p.jpg"
//
//let test =
//    let bm = LoadBitmap f 
////bm|>CloneButLowerSize |> SaveBitmap <| f_
//    //bm |> compress 0.3  |>  SaveBitmap f_
//    bm|> SaveBitmap f_ 75L
//    bm.Dispose()
    //|>CloneButLowerSize PixelFormat.Format24bppRgb